using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using ApiTest.Api.Entity;
using System.Linq;
using ApiTest.Api.Db;
using ApiTest.Api.Repository;
using ApiTest.Api.Params;
using Microsoft.Extensions.Configuration;
using ApiTest.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Admin3000.Backend.Api.Params;

namespace ApiTest.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class SpuController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Spu> _SpuRepository;
        private TokenParameter _tokenParameter;

        public SpuController(IConfiguration configuration, IRepository<Spu> SpuRepository)
        {
            _configuration = configuration;
            _SpuRepository = SpuRepository;
            _tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();
        }

        //列出分类列表
        [HttpGet]
        public dynamic Get()
        {
            var spu = _SpuRepository.Table.ToList();
            var res = new
            {
                Code = 1000,
                Data = new { Data = spu },
                Msg = "获取列表成功^_^"
            };

            return JsonHelper.Serialize(res);
        }


        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var spu = _SpuRepository.GetById(id);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = spu,
                Msg = "查询指定用户成功"
            });
        }

        [HttpPost]
        public dynamic Post(CreateSpu newSpu)
        {
            var Spu = new Spu
            {
                Spuname = newSpu.Spuname,
                Price = newSpu.Price,
                Brand = newSpu.Brand,
                SubclassifyId = newSpu.SubclassifyId,
                ShopId = newSpu.ShopId,
                Num = newSpu.Num,
                Intro = newSpu.Intro,
                ImageId=newSpu.ImageId
            };
            _SpuRepository.Insert(Spu);
            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = Spu,
                Msg = "添加商品成功"
            });
        }
        [HttpPut("{id}")]
        public dynamic Put(int id, CreateSpu updateSpu)
        {
            var Spuname = updateSpu.Spuname.Trim();
            var Price = updateSpu.Price.Trim();
            var Brand = updateSpu.Price.Trim();
            var spu = _SpuRepository.GetById(id);
            if (spu == null)
            {
                return JsonHelper.Serialize(new
                {
                    Code = 104,
                    Data = "",
                    Msg = "查询不到该用户，无法更新，请重试"
                });
            }

            spu.Spuname = updateSpu.Spuname;
            spu.Price=updateSpu.Price;
            spu.Brand=updateSpu.Brand;
            spu.ShopId=updateSpu.ShopId;
            spu.Num=updateSpu.Num;
            spu.Intro=updateSpu.Intro;
            spu.ImageId =updateSpu.ImageId;
         
            // user.IsActived= updateUser.IsActived;

            _SpuRepository.Update(spu);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = spu,
                Msg = "更新成功"
            });


        }

        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            _SpuRepository.Delete(id);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除成功"
            };


        }
    }

}
